define([
    "dojo/_base/declare",
    "dijit/_Widget",
    "dijit/_Templated",
    "dojo/text!./SupplyGrid/template/grid.html",
    "dojo/_base/kernel",
    "dojox/grid/DataGrid",
    "dojo/data/ItemFileWriteStore",
    "dojo/behavior",
    "dijit/form/TextBox",
    "dijit/form/Select",
    "dijit/form/Button",
    "dijit/form/SimpleTextarea"],
    function(declare, _Widget,_Templated,templateHtml,dojo,DataGrid,ItemFileWriteStore,Behavior){
        return declare("widget.SupplyGrid",[_Widget,_Templated], {
            widgetsInTemplate: true,
            baseClass: "supply-grid",
            templateString: templateHtml,
            grid:null,
            initGrid:function(){
                var layout = [[
                    {
                        'name': 'Ref#', 
                        'field': 'Ref', 
                        'width': "12.5%",
                        formatter: function(text,row,object){
                            var id = object.grid.getItem(row).ID[0];
                            return "<a class='view' supply='"+id+"' href='javascript:void(0)'>"+text+"</a>";
                        }
                    },

                    {
                        'name': 'Photo', 
                        'field': 'Photo' ,
                        'width': "50px",
                        formatter: function(text){
                            if (text){
                                return "<img width='50px' height='50px' src='"+djConfig.image + text +"' />";
                            }else{
                                return "";
                            }
                        }
                    },

                    {
                        'name': 'Name', 
                        'width': "12.5%",
                        'field': 'Name'
                    },

                    {
                        'name': 'Supplier', 
                        'width': "12.5%",
                        'field': 'Supplier'
                    },

                    {
                        'name': 'Category', 
                        'width': "12.5%",
                        'field': 'Category'
                    },

                    {
                        'name': 'Price', 
                        'width': "12.5%",
                        'field': 'Price'
                    },

                    {
                        'name': 'Exception', 
                        'width': "12.5%",
                        'field': 'Exception'
                    },

                    {
                        'name': ' ',
                        'width': "12.5%",
                        'field': 'ID',
                        formatter: function(text){
                            return "<a class='edit' supply='"+text+"' href='javascript:void(0)'>Edit</a>&nbsp;&nbsp;<a supply='"+text+"' class='delete' href='javascript:void(0)'>Remove</a>";
                        }
                    }
                ]];
                this.grid = new DataGrid({
                    id: 'supply_grid',
                    structure: layout,
                    selectionMode:"none",
                    autoHeight: true,
                    onHeaderClick : function(){
                        Behavior.apply();
                    }
                },
                document.createElement('div'));
                dojo.byId("supply_grid_div").appendChild(this.grid.domNode);
                this.grid.startup();
            },
            
            loadGrid: function(){
                var data = {};
                var xhrArgs = {
                    url:"supply/getsupplylist",
                    handleAs: "json"
                };
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(dojo.hitch(this,function(result){
                    data.identifier = 'ID';
                    data.items = result;
                    var store = new ItemFileWriteStore({data: data});
                    this.grid.setStore(store);
                    Behavior.apply();
                }));
            },
            
            bindAction:function(){
                var that = this;
                Behavior.add({
                    ".supply-grid a.view" : {
                        onclick: function(){
                            var id = dojo.getAttr(this,"supply");
                            that.viewSupply(id);
                        }
                    },
                    ".supply-grid a.edit" : {
                        onclick: function(){
                            var id = dojo.getAttr(this,"supply");
                            that.editSupply(id);
                        }
                    },
                    ".supply-grid a.delete" : {
                        
                        onclick: function(){
                            var id = dojo.getAttr(this,"supply");
                            that.deleteSupply(id);
                        }
                    }
                });
                
                dojo.connect(this.newButton,"onClick",function(){
                    that.newSupply();
                });
                
                dojo.connect(this.exportButton,"onClick",dojo.hitch(this,function(){
                    location.href = "supply/export";
                }));
                
                dojo.subscribe("SupplyList/Show", null,dojo.hitch(this,function(object){
                    this.loadGrid();
                    dojo.style(this.domNode, "display", "block");
                }));
                
                dojo.subscribe("Product2Supply/Show", null,dojo.hitch(this,function(object){
                    this.getParent().getParent().selectChild(this.getParent());
                    this.viewSupply(object.id);
                }));
            },
            
            newSupply:function(){
                dojo.style(this.domNode, "display", "none");
                dojo.publish("SupplyForm/Show",{mode:'new'});
            },
            
            viewSupply: function(id){
                dojo.style(this.domNode, "display", "none");
                dojo.publish("SupplyForm/Show", {mode:'view',id:id});
            },
            
            editSupply: function(id){
                dojo.style(this.domNode, "display", "none");
                dojo.publish("SupplyForm/Show", {mode:'edit',id:id});
            },
            
            deleteSupply: function(id){
                var xhrArgs = {
                    url:"supply/deletesupply/sid/"+id,
                    handleAs: "json"
                };
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(dojo.hitch(this,function(result){
                    this.loadGrid();
                }));
            },
            
            postCreate: function(){
                this.inherited(arguments);
                this.initGrid();
                this.loadGrid();
                this.bindAction();
            }
        });
    });